package com.infore.water.controller;

import com.infore.water.service.UserService;
import com.infore.water.util.ListObject;
import com.infore.water.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author :wk
 */
@RestController
public class UserControl {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param account 用户名
     * @param password 密码
     * @return ListObject
     */
    @RequestMapping("/login")
    public ListObject login(@RequestParam(value = "account")String account,
                            @RequestParam(value = "password")String password){
        try{
            Map<String,Object> map =  userService.queryUserByAccountPassword(account,password);
            return ResponseUtil.queryListObject(map,"登录成功");
        }catch (EmptyResultDataAccessException e){
            e.printStackTrace();
            return ResponseUtil.error("用户名或密码错误");
        }
    }

    @RequestMapping("/changePassword")
    public ListObject changePassword(@RequestParam(value = "userId")String userId,
                                     @RequestParam("password")String password,
                                     @RequestParam("newPassword")String newPassword){
        int num = userService.changePassword(userId,password,newPassword);
        if(num==-1){
            return ResponseUtil.error("原始密码错误");
        }else if (num!=0){
            return ResponseUtil.success();
        }else {
            return ResponseUtil.error();
        }
    }
}
